/**
 * @Title: chartLegendGroup_directive.js
 * @Description: TODO
 * Copyright: Copyright (c) 2014 
 * Company:中国平安
 *
 * @Project HIMS-FWA
 * @author QIANDUO875
 * @date 2014年10月13日 下午6:42:46
 * @version V1.0
 */
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.directives');
	app.directive('fwaLegendGroup', [function(){
		
		var tmpl = '\
			<ul class="legend-group col-xs-12">\
				<li class="legend-group-name">\
					<a ng-click="close()">&times;</a> {{title}}:\
				</li>\
				<li ng-repeat="item in items" ng-click="onItemClick(item)" class="pull-right">\
					<div class="legend-item-block" ng-show="item.hide" style="background-color:#eee;"></div>\
					<div class="legend-item-block" ng-hide="item.hide" style="background-color:{{item.color}};"></div>\
					<div class="legend-item-title" title="{{item.name}}" ng-class="{\'disabled\':item.hide}">\
						<span ng-bind="item.name"></span>\
					</div>\
				</li>\
			</ul>';
		
		function link($scope, $elm, $attrs, $ctrl) {
			var dataName = $attrs.groupData;
			var data;
			
			$scope.$parent.$watch(dataName, function(v){
				if(v) {
					data = v;
					$scope.title = v.title;
					$scope.items = v.items;
				}
			});
			
			$scope.onItemClick = function(item) {
				item.hide = item.hide === true?false:true;
				$scope.$parent.$emit('fwaLegendGroup.legendStateChange',item);
			}
			
			$scope.close = function() {
				$scope.$parent.$emit('fwaLegendGroup.close',data);
				$scope.$destroy();
			}
		}
		
		return {
			restrict: 'E',
			template:tmpl,
			scope:true,
			replace:true,
			link:link
		};
		
	}]);
	
})();